package cn.udrm.water.base.v1.waterdistribute.restful;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.udrm.water.base.v1.commons.models.PageModel;
import cn.udrm.water.base.v1.commons.models.RequestListParam;
import cn.udrm.water.base.v1.commons.models.ResponseParam;
import cn.udrm.water.base.v1.waterdistribute.dto.WaterGateVendorDto;
import cn.udrm.water.base.v1.waterdistribute.service.WaterGateVendorService;

@Api(tags="闸门厂商实体API", description="闸门厂商CRUD操作")
@RestController
@RequestMapping(value="v1/restful/waterGateVendor")
public class WaterGateVendorRestful {
	@Autowired
	private WaterGateVendorService gateVendorService;
	
	@ApiOperation("获取指定闸门厂商下的闸门")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="闸门厂商GUID")
	@GetMapping(value="getWaterGates/{id}")
	public ResponseParam<WaterGateVendorDto> getWaterGates(@PathVariable(value="id") String id) {
		WaterGateVendorDto withWaterGates = gateVendorService.getWithWaterGates(id);
		return new ResponseParam<WaterGateVendorDto>("数据获取成功", 200, withWaterGates);
	}
	
	@ApiOperation("列表数据获取")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="body", name="requestListParam", required=false, value="列表查询条件", dataType="RequestListParam«WaterGateVendorDto»")
	})
	@PostMapping(value="list")
	public ResponseParam<PageModel<WaterGateVendorDto>> list(@RequestBody(required=false) RequestListParam<WaterGateVendorDto> requestListParam) throws UnsupportedEncodingException {
		if (requestListParam == null) {
			requestListParam = new RequestListParam<>();
		}
		PageModel<WaterGateVendorDto> pageByConditions = gateVendorService.getPageByConditions(requestListParam.getNumber()
													,requestListParam.getSize()
													,requestListParam.getEntity()
													,requestListParam.getSortField()
													,requestListParam.getSortType());
		return new ResponseParam<PageModel<WaterGateVendorDto>>("数据获取成功", 200, pageByConditions);
	}
	
	@ApiOperation("单条数据获取")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="闸门GUID")
	@GetMapping(value="get/{id}")
	public ResponseParam<WaterGateVendorDto> get(@PathVariable(value="id") String id) {
		WaterGateVendorDto waterGateVendorDto = gateVendorService.getAllById(id);
		return new ResponseParam<WaterGateVendorDto>("数据获取成功", 200, waterGateVendorDto);
	}
	
	@ApiOperation("闸门厂商数据保存")
	@ApiImplicitParam(name="waterGateVendorDto", value="需要添加的闸门厂商", required=true, dataType="WaterGateVendorDto", dataTypeClass=WaterGateVendorDto.class)
	@PostMapping(value="save")
	public ResponseParam<WaterGateVendorDto> save(@RequestBody WaterGateVendorDto waterGateVendorDto) {
		waterGateVendorDto = gateVendorService.save(waterGateVendorDto);
		
		return new ResponseParam<WaterGateVendorDto>("数据保存成功", 200, waterGateVendorDto);
	}
	
	@ApiOperation("闸门厂商数据更新")
	@ApiImplicitParam(name="waterGateVendorDto", value="需要更新的闸门厂商", required=true, dataType="WaterGateVendorDto", dataTypeClass=WaterGateVendorDto.class)
	@PostMapping(value="update")
	public ResponseParam<WaterGateVendorDto> update(@RequestBody WaterGateVendorDto waterGateVendorDto) {
		waterGateVendorDto = gateVendorService.update(waterGateVendorDto);
		
		return new ResponseParam<WaterGateVendorDto>("数据更新成功", 200, waterGateVendorDto);
	}

	@ApiOperation("闸门厂商数据删除（仅删除自身，不能删除标识有实体间关系）")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="闸门厂商GUID")
	@GetMapping(value="deleteForce/{id}")
	public ResponseParam<String> deleteForce(@PathVariable(value="id") String id) {
		try {
			gateVendorService.deleteAllSelf(new String[]{id});
		} catch (Exception e) {
			return new ResponseParam<String>("删除失败", 500);
		}
		return new ResponseParam<String>("删除成功", 200);
	}
}
